﻿using System.Text;
using HtmlAgilityPack;

namespace RatingAuthor
{
  public struct Page : IWorkItem
  {
    public string FileName { get; set; }
    public int Index { get; set; }
    public bool IsValid { get { return FileName != null; } }

    public Page(string fileName, int index) : this()
    {
      FileName = fileName;
      Index = index;
    }

    public Page FixEncodingAndCache(PagesCache pagesCache)
    {
      var doc = new HtmlDocument();
      doc.DetectEncodingAndLoad(FileName);
      var cacheFileName = pagesCache.GetFileName(Index);
      doc.Save(cacheFileName, Encoding.Unicode);
      return new Page(cacheFileName, Index);
    }

    public HtmlDocument Load()
    {
      var doc = new HtmlDocument();
      doc.Load(FileName, Encoding.Unicode);
      return doc;
    }
  }
}